# =======================================================================================================

# 2. python3 安装

# 参考：https://www.runoob.com/python3/python3-install.html
#
# 参考：强制删除组件
# rpm -e --nodeps {组件名称}
#  
# python 3.8.1 编译报错 Could not import runpy module 问题
# 原因：./configure --enable-optimizations，configure命令后面带上了--enable-optimizations
# https://blog.90.vc/archives/367
#
# shell下cat EOF中变量$处理
# https://blog.csdn.net/ocean_1991/article/details/103895174
# 
# python 2.x/3.x 同时安装 == windows
# https://www.cnblogs.com/zhengyihan1216/p/6011640.html
# https://www.cnblogs.com/wxy2000/p/12396711.html
# 
# python 2.x/3.x 同时安装 == linux
# https://www.cnblogs.com/royfans/p/13164398.html  【推荐】
# https://www.cnblogs.com/zhouwanchun/p/12976998.html
# https://www.cnblogs.com/l5623064/p/8690945.html
# https://jingyan.baidu.com/article/642c9d3435a1e9644a46f739.html
# https://blog.csdn.net/watfe/article/details/52755076
# 
# python 官方教程 == 安装pip
# https://docs.python.org/zh-cn/2.7/installing/index.html
# 

# python 2.x 安装完之后，提示的信息，如下： ----------------begin -----------------------
# 解决办法：
# https://blog.csdn.net/huanle0610/article/details/41174943
# https://www.jianshu.com/p/6be231fc2646
# 
# yum install -y bsddb ncurses
# yum install -y sqlite sqlite-devel bz2
# yum install -y bzip2-devel bsddb gdbm-devel
# yum install -y readline-devel zlib
# 
# Python build finished, but the necessary bits to build these modules were not found:
# _bsddb             _curses            _curses_panel   
# _sqlite3           _tkinter           bsddb185        
# bz2                dbm                dl              
# gdbm               imageop            readline        
# sunaudiodev                                           
# To find the necessary bits, look in setup.py in detect_modules() for the module's name.  
# python 2.x 安装完之后，提示的信息，如下： ----------------end ----------------------- 


# 安装pip, 和国内python / pip的镜像源  【推荐】
# https://www.runoob.com/w3cnote/python-pip-install-usage.html  【推荐】
# https://www.cnblogs.com/luocodes/p/13833037.html  
# https://blog.csdn.net/wickedvalley/article/details/52015878   
# https://www.cnblogs.com/Jimc/p/10218062.html


# python升级带来的yum异常：File "/usr/bin/yum", line 30
# yum包管理是使用python2.x写的， 修改yum配置文件，将python版本指向以前的旧版本
# https://blog.csdn.net/z13615480737/article/details/78670228
# https://www.cnblogs.com/grimm/p/11103902.html  
# 操作步骤
# step 1: 复制旧版本python 2.x，如下：
# cp /usr/bin/python2.7_bak_2.7.5  /usr/bin/python2.7
# 
# step 2: 文件1 ： 修改/usr/bin/yum
# vi /usr/bin/yum
# 替换如下内容
# old 内容 = #!/usr/bin/python
# new 内容 = #!/usr/bin/python2.7
# 
# tep 3: 文件2 ： 修改/usr/libexec/urlgrabber-ext-down
# vi /usr/libexec/urlgrabber-ext-down
# 替换如下内容
# old 内容 = #!/usr/bin/python
# new 内容 = #!/usr/bin/python2.7

# pip 官方网站
# https://pypi.org/project/pip/
 
# pip 命令行  【推荐】
# pip list --outdated：可以查看有新版本的第三方库，可显示现在安装的版本，以及最新的版本
# https://www.jianshu.com/p/9882621bfbeb

# pip 配置源地址 == 配置国内源地址，加速pip install 下载和安装软件的速度
# https://www.cnblogs.com/schut/p/10410087.html

# =======================================================================================================

# 修改yum为国内镜像 === 看具体情况，有时候国内镜像不一定完整，这个时候还是要切换回国外地址
cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak && \
wget -O /etc/yum.repos.d/CentOS-Base.repo -L http://mirrors.aliyun.com/repo/Centos-7.repo

yum clean all && \
yum makecache

# 清理掉无用的repo
yum --enablerepo=base clean metadata

# =======================================================================================================

# 切记：首先，安装依赖库
yum install -y autoconf automake make && \
yum install -y gcc gcc-c++
# yum install -y tcl-devel tk-devel


# 切记：python 3.x 需要的类库如下：
yum install -y zlib zlib-devel && \
yum install -y openssl-devel && \
yum install -y glibc-static libstdc++-static && \
yum install -y libffi-devel # Python3中有个内置模块叫ctypes，它是Python3的外部函数库模块，它提供兼容C语言的数据类型

# django默认数据库为sqlite3，所以安装
# yum install -y sqlite-devel
 
# =======================================================================================================

# 下载python源代码
mkdir -p /opt/soft/python/
wget -O /opt/soft/python/Python-3.7.2.tgz -L https://www.python.org/ftp/python/3.7.2/Python-3.7.2.tgz
tar -zxvf /opt/soft/python/Python-3.7.2.tgz -C /opt/soft/python/

# 
# 构建python源代码
cd /opt/soft/python/Python-3.7.2

mkdir -p /usr/local/python-3.7.2
# ./configure
# ./configure --enable-optimizations
# 或 ./configure --prefix=/usr/local/python-3.7.2  --enable-optimizations
./configure --prefix=/usr/local/python-3.7.2

# 或者，当上面configure方法不行时候，用如下命令
# ./configure --enable-shared --enable-loadable-sqlite-extensions -with-zlib
# ./configure --enable-shared --enable-loadable-sqlite-extensions -with-zlib --prefix=/usr/local/python-3.7.2

# 编译源代码
# 或，原因有些高版本的python，加了-j参数后，可能会构建失败提示缺少numpy类库或其他报错 
# make
make -j2
# make check
# 安装python
make install


# =======================================================================================================

# 检查python 是否安装成功

# 检查python-2.x 版本
# 如果安装成功，则输出 Python 3.7.2
cd /usr/local/python-3.7.2/bin/ # 参考上面configure指定的install 目录
./python3.7 --version 
# 输出结果如下：
# [root@CNT7XDEV02 Python-3.7.2]# /usr/local/python-3.7.2/bin/python3.7 --version
# Python 3.7.2

# 查看已有的python版本
ls -al /usr/lib64 | grep python
# -rwxr-xr-x.  1 root root 1847240 Aug  4  2017 libpython2.7.so.1.0
# drwxr-xr-x. 26 root root   20480 Feb 25  2019 python2.7
ls -al /usr/bin | grep python
# lrwxrwxrwx.  1 root root         7 Feb 25  2019 python -> python2
# lrwxrwxrwx.  1 root root         9 Feb 25  2019 python2 -> python2.7
# -rwxr-xr-x.  1 root root      7136 Aug  4  2017 python2.7

# 首先备份系统默认安装的python 2.7.5
# mv /usr/bin/python   /usr/bin/python_bak_2.7.5        # 防止yum命令无法使用，因为yum 软件默认只支持python-2.x
# mv /usr/bin/python2   /usr/bin/python2_bak_2.7        # 防止yum命令无法使用，因为yum 软件默认只支持python-2.x
# mv /usr/bin/python2.7   /usr/bin/python2.7_bak_2.7.5  # 防止yum命令无法使用，因为yum 软件默认只支持python-2.x
# 建立软连接  /usr/local/bin/
# ln -s /usr/local/python-3.7.2/bin/python3.7   /usr/bin/python
ln -s /usr/local/python-3.7.2/bin/python3.7   /usr/bin/python3
ln -s /usr/local/python-3.7.2/bin/python3.7   /usr/bin/python3.7
ln -s /usr/local/python-3.7.2/bin/pip3   /usr/bin/pip3
ln -s /usr/local/python-3.7.2/bin/pip3.9   /usr/bin/pip3.7

# 检查安装结果：然后查看/usr/bin/ 下面的python命令，如下：_bak的后缀表示禁用的历史python-2.7.5版本
ls -al /usr/bin | grep python
# 输出结果，如下：
# [root@CNT7XDEV04 bin]# ls -al /usr/bin | grep python
# lrwxrwxrwx.  1 root root         7 Feb 25  2019 python -> python2
# lrwxrwxrwx.  1 root root         9 Feb 25  2019 python2 -> python2.7
# -rwxr-xr-x.  1 root root      7136 Aug  4  2017 python2.7
# lrwxrwxrwx   1 root root        37 Feb 18 12:49 python3 -> /usr/local/python-3.7.2/bin/python3.7
# lrwxrwxrwx   1 root root        37 Feb 18 12:49 python3.7 -> /usr/local/python-3.7.2/bin/python3.7

# 检查安装结果：
python --version
python3 --version
python3.7 --version
pip3 --version
which python
which python3
which python3.7
which pip3
# 输出结果，如下：
# [root@CNT7XDEV04 bin]# python --version
# Python 2.7.5
# [root@CNT7XDEV04 bin]# python3 --version
# Python 3.7.2
# [root@CNT7XDEV04 bin]# python3.7 --version
# Python 3.7.2
# [root@CNT7XDEV04 ~]# pip3 --version
# pip 18.1 from /usr/local/python-3.7.2/lib/python3.7/site-packages/pip (python 3.7)
# [root@CNT7XDEV04 bin]# which python
# which python3
# /usr/bin/python
# [root@CNT7XDEV04 bin]# which python3
# /usr/bin/python3
# [root@CNT7XDEV04 bin]# which python3.7
# /usr/bin/python3.7
# [root@CNT7XDEV04 ~]# which pip3
# /usr/local/python-3.7.2/bin/pip3

# =======================================================================================================

# 设置python的环境变量, 参考：shell下cat EOF中变量$处理
# 设置python-3.x
cat >> /etc/profile  << "EOF"

PYTHON_HOME_3_9_1=/usr/local/python-3.7.2
PATH=$PATH:$PYTHON_HOME_3_9_1/bin
CLASSPATH=$CLASSPATH:$PYTHON_HOME_3_9_1/lib
EOF


# 生效profile设置
source /etc/profile

# =======================================================================================================

# 【重要】切记：
# 配资pip 的国内源地址，方便加快pip install下载和安装软件的速度

# 在 .pip目录下创建一个 pip.conf 文件
mkdir ~/.pip
touch ~/.pip/pip.conf
# 输入如下内容，国内pip源地址
# 国内镜像地址，参考如下：
# 清华大学：https://pypi.tuna.tsinghua.edu.cn/simple
# 阿里云：http://mirrors.aliyun.com/pypi/simple/
# 豆瓣：http://pypi.douban.com/simple/
# 
# 备注： # trusted-host 此参数是为了避免麻烦，否则使用的时候可能会提示不受信任
cat > ~/.pip/pip.conf << 'EOF'
[global] 
index-url = https://mirrors.aliyun.com/pypi/simple
[install]
trusted-host = https://mirrors.aliyun.com
EOF

# 更新一下pip3版本
pip3 --version
# 输出结果为：pip 21.0.1 from /usr/local/python-3.9.1/lib/python3.9/site-packages/pip (python 3.9)
# 更新pip3版本，如下操作
# python3.9 -m pip install --upgrade pip
python3 -m pip install --upgrade pip
# 再次查看版本，如下操作
pip3 --version
# 输出结果为：pip 20.2.3 from /usr/local/python-3.9.1/lib/python3.9/site-packages/pip (python 3.9)

# =======================================================================================================

# 安装组件：virtualenv
# 执行命令：pip3 install virtualenv
# 错误提示：You should consider upgrading via the '/usr/bin/python3.9 -m pip install --upgrade pip' command.
# 
# 操作步骤，如下：
# 执行如下命令，更新pip3版本，即可
# python3.9 -m pip install --upgrade pip
# python3 -m pip install --upgrade pip
# [root@CNT7XDEV03 ~]# python3.9 -m pip install --upgrade pip
# Looking in indexes: https://mirrors.aliyun.com/pypi/simple
# Collecting pip
#   Downloading https://mirrors.aliyun.com/pypi/packages/fe/ef/60d7ba03b5c442309ef42e7d69959f73aacccd0d86008362a681c4698e83/pip-21.0.1-py3-none-any.whl (1.5 MB)
#      |鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅| 1.5 MB 9.5 MB/s 
# Installing collected packages: pip
#   Attempting uninstall: pip
#     Found existing installation: pip 20.2.3
#     Uninstalling pip-20.2.3:
#       Successfully uninstalled pip-20.2.3
# Successfully installed pip-21.0.1
# 
# 查看安装后的组件：virtualenv
# pip3 list
# pip3 list | grep virtualenv
# 输出结果如下：
# [root@CNT7XDEV03 ~]# pip3 list
# Package    Version
# ---------- -------
# appdirs    1.4.4
# distlib    0.3.1
# filelock   3.0.12
# pip        21.0.1
# setuptools 49.2.1
# six        1.15.0
# virtualenv 20.4.2
# [root@CNT7XDEV03 ~]# 
# [root@CNT7XDEV03 ~]# pip3 list | grep virtualenv
# virtualenv 20.4.2